define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'otc/index' + location.search,
                    add_url: 'otc/add',
                    edit_url: 'otc/edit',
                    //del_url: 'otc/del',
                    multi_url: 'otc/multi',
                    table: 'user_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: '用户ID'},
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'trade_no', title: __('Trade_no')},
                        {field: 'order_type', title: __('Order_type'), searchList: {"1":__('Order_type 1'),"2":__('Order_type 2'),"3":__('Order_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'order_status', title: __('Order_status'), searchList: {"0":__('Order_status 0'),"1":'已转账',"2":__('Order_status 2'),"3":__('Order_status 3'),"4":__('Order_status 4')}, formatter: Table.api.formatter.status},
                        //{field: 'coin_type', title: __('Coin_type'), searchList: {"1":__('Coin_type 1'),"2":__('Coin_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'coin_num', title: __('Coin_num'), operate:'BETWEEN'},
                        {field: 'sell_price', title: __('Sell_price'), operate:'BETWEEN'},
                        {field: 'transfer_fee', title: __('Transfer_fee'), operate:'BETWEEN'},
                        {field: 'account_type', title: '收款类型', operate:'false'},
                        {field: 'account_name', title: '收款用户名', operate:'false'}, 
                        {field: 'account_number', title: '收款账号', operate:'false'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'checktime', title: __('Checktime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'paytime', title: '到账时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [
                                {   
                                    visible: function (row) {
                                        if(row.collection_type==1 && row.order_type==3 && row.order_status==4){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'ajax',
                                    text: '微信转账',
                                    title: '微信转账',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-wechat',
                                    url: 'otc/transfers',
                                    confirm: '确认微信转账吗?',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {   
                                    visible: function (row) {
                                        if(row.collection_type==2 && row.order_type==3 && row.order_status==4){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'ajax',
                                    text: '确认打款',
                                    title: '确认打款',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-credit-card-alt',
                                    url: 'otc/bank',
                                    confirm: '确认已打款吗?',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    visible: function (row) {
                                        if(row.order_status==3 && row.order_type==3){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'edit',
                                    text: '订单审核',
                                    title: '订单审核',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'otc/edit',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                            ],
                         formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'otc/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'otc/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'otc/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});